/**
 * Created by kenz on 2018/4/10.
 */
import React, { Component } from 'react'
import { connect } from 'react-redux'
import { addTodo } from '../actions'
import { bindActionCreators } from 'redux'


class AddTodo extends Component {
  constructor(...args) {
    super(...args)
    this.state = {
      textVal: ''
    }
  }
  bindInput(e) {
    this.setState({
      textVal: e.target.value
    })
  }
  handleAdd() {
    this.props.addTodo(this.state.textVal)
    this.setState({
      textVal: ''
    })
  }
  render() {
    return (
      <div>
        <input type="text" value={this.state.textVal} onChange={this.bindInput.bind(this)} />
        <button onClick={this.handleAdd.bind(this)}>添加</button>
      </div>
    )
  }
}


const mapStateToProps = state => ({
  todos: state.todos
})

const mapDispatchToProps = dispatch => ({
  addTodo: bindActionCreators(addTodo, dispatch)
})

export default connect(
  mapStateToProps,
  mapDispatchToProps
)(AddTodo)